﻿/*************************************************************************************
     * CLR 版本：       4.0.30319.42000
     * 类 名 称：       CsExcelRead
     * 机器名称：       KAIKO
     * 命名空间：       PunchCard.Service
     * 文 件 名：       CsExcelRead
     * 创建时间：       2022/4/11 15:24:41
     * 作    者：       WongJunLiang
     * 说    明：
     * 修改时间：
     * 修 改 人：
*************************************************************************************/


using PunchCard.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using static PunchCard.Model.ModelMainWindow;
using MSExcel = Microsoft.Office.Interop.Excel;

namespace PunchCard.Service
{
    class CsExcelRead
    {
        public static void ex_Read(string strFilePath)
        {
            MSExcel.Application excelApp = new MSExcel.Application();
            MSExcel._Workbook workbook = excelApp.Workbooks.Add(strFilePath);
            MSExcel.Worksheet worksheet = (MSExcel.Worksheet)workbook.Worksheets.get_Item(1);//读取第一张表，以顺序
                                                                                             //MSExcel.Worksheet worksheet = (MSExcel.Worksheet)workbook.Worksheets["sheet"];//读取第一张表，以表名
            {
                string strID = ex_ReadContext(ref worksheet, 1, 1);//第一行第一列，该单元格文本
            }

            workbook.Close();//关闭文档
            excelApp.Quit();//关闭excel应用程序
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);//释放excel进程
            excelApp = null;
        }

        public static string ex_ReadContext(ref MSExcel.Worksheet worksheet, int nRow, int nCol)
        {
            string strReadContext = string.Empty;
            strReadContext = ((MSExcel.Range)worksheet.Cells[nRow, nCol]).Text;//第一行第一列，该单元格文本
            return strReadContext;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static void ex_Read_1(ref ObservableCollection<CsListINF> Litem, ref FileParam param)
        {
            MSExcel.Application excelApp = new MSExcel.Application();
            MSExcel._Workbook workbook = excelApp.Workbooks.Add(param.FilePath);
            MSExcel.Worksheet worksheet = (MSExcel.Worksheet)workbook.Worksheets.get_Item(1);//读取第一张表，以顺序
                                                                                             //MSExcel.Worksheet worksheet = (MSExcel.Worksheet)workbook.Worksheets["sheet"];//读取第一张表，以表名
            {//处理表内内容
                List<string> LTime = new List<string>() { "8:30", "12:00", "13:30", "18:00" };
                int nRowCnt = worksheet.UsedRange.CurrentRegion.Rows.Count;//累计行数
                string strCurrent = ex_ReadContext(ref worksheet, 2, 7);//该单元格文本
                if (strCurrent != "第一段上班时间")
                {
                    MessageBox.Show("考勤异常表中，第一段上班时间列表不在第7列，请调整");
                }
                else if (nRowCnt > 0)
                {
                    for (int i = 3; i < nRowCnt + 2; i++)//从第二行开始
                    {
                        string strWorkerID = ex_ReadContext(ref worksheet, i, 1);//该单元格文本
                        if (strWorkerID.CompareTo(param.FileID) == 0)
                        {
                            string strName = ex_ReadContext(ref worksheet, i, 2);//该单元格文本
                            string strDate = ex_ReadContext(ref worksheet, i, 3);//该单元格文本
                            for (int j = 0; j < 4; j++)
                                if (ex_ReadContext(ref worksheet, i, j + 7) == "")
                                {
                                    string strNameID = strName + "_" + strWorkerID;
                                    CsListINF listINF = new CsListINF()
                                    {
                                        strID = (Litem.Count + 1).ToString(),
                                        strName = strNameID,
                                        strDate = strDate,
                                        strTime = LTime[j],
                                        strType = "正常签卡",
                                        strReason = param.Reason
                                    };
                                    Litem.Add(listINF);
                                }
                            param.FileID = strWorkerID;
                            param.FileName = strName;
                        }
                    }
                }


            }
            workbook.Close();//关闭文档
            excelApp.Quit();//关闭excel应用程序
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);//释放excel进程
            excelApp = null;
        }

        public static bool ex_Read_1_Pro(ref MSExcel.Worksheet worksheet, ref ObservableCollection<CsListINF> Litem, FileParam param)
        {
            {//处理表内内容
                List<string> LTime = new List<string>() { "8:30", "12:00", "13:30", "18:00" };
                int nRowCnt = worksheet.UsedRange.CurrentRegion.Rows.Count;//累计行数
                string strCurrent = ex_ReadContext(ref worksheet, 2, 7);//该单元格文本
                if (strCurrent != "第一段上班时间")
                {
                    MessageBox.Show("考勤异常表中，第一段上班时间列表不在第7列，请调整");
                    return false;
                }
                else if (nRowCnt > 0)
                {
                    for (int i = 3; i < nRowCnt + 2; i++)//从第二行开始
                    {
                        string strWorkerID = ex_ReadContext(ref worksheet, i, 1);//该单元格文本
                        if (strWorkerID.CompareTo(param.FileID) == 0)
                        {
                            string strName = ex_ReadContext(ref worksheet, i, 2);//该单元格文本
                            string strDate = ex_ReadContext(ref worksheet, i, 3);//该单元格文本
                            for (int j = 0; j < 4; j++)
                                if (ex_ReadContext(ref worksheet, i, j + 7) == "")
                                {
                                    string strNameID = strName + "_" + strWorkerID;
                                    CsListINF listINF = new CsListINF()
                                    {
                                        strID = (Litem.Count + 1).ToString(),
                                        strName = strNameID,
                                        strDate = strDate,
                                        strTime = LTime[j],
                                        strType = "正常签卡",
                                        strReason = param.Reason
                                    };
                                    Litem.Add(listINF);
                                }
                            param.FileID = strWorkerID;
                            param.FileName = strName;
                        }
                    }
                }
            }
            return true;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static void ex_Read_2(ref ObservableCollection<CsListINF> Litem, ref FileParam param)
        {
            MSExcel.Application excelApp = new MSExcel.Application();
            MSExcel._Workbook workbook = excelApp.Workbooks.Add(param.FilePath);
            MSExcel.Worksheet worksheet = (MSExcel.Worksheet)workbook.Worksheets.get_Item(1);//读取第一张表，以顺序
                                                                                             //MSExcel.Worksheet worksheet = (MSExcel.Worksheet)workbook.Worksheets["sheet"];//读取第一张表，以表名
            {//处理表内内容
                Dictionary<string, List<string>> m_mapList = new Dictionary<string, List<string>>();

                int nRowCnt = worksheet.UsedRange.CurrentRegion.Rows.Count;//累计行数
                string strWorkerID = ex_ReadContext(ref worksheet, 2, 1);//该单元格文本
                string strName = ex_ReadContext(ref worksheet, 2, 2);//该单元格文本
                param.FileID = strWorkerID;
                param.FileName = strName;
                if (nRowCnt > 0)
                {
                    for (int i = 2; i < nRowCnt + 1; i++)//从第二行开始
                    {
                        string strTime = ex_ReadContext(ref worksheet, i, 4);//该单元格文本
                        string strActTime = ex_ReadContext(ref worksheet, i, 3);//该单元格文本
                        if (m_mapList.ContainsKey(strTime))
                        {
                            m_mapList[strTime].Add(strActTime);
                        }
                        else
                        {
                            m_mapList.Add(strTime, new List<string>() { strActTime });
                        }
                    }
                    ListCompare(strName, strWorkerID, param.Reason, ref m_mapList, ref Litem);
                }
            }

            workbook.Close();//关闭文档
            excelApp.Quit();//关闭excel应用程序
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);//释放excel进程
            excelApp = null;
        }

        private static void ListCompare(string strParam1, string strParam2, string strParam3,
            ref Dictionary<string, List<string>> mapList, ref ObservableCollection<CsListINF> Litem)
        {
            var LMonth = ViewModel.ViewModel.GetInstance().m_Model_MainWindow.m_ListMonth;
            List<string> LTime = new List<string>() { "8:30", "12:00", "13:30", "18:00" };
            foreach (var item in LMonth)
            {
                if (mapList.ContainsKey(item))
                {
                    var nFlag = IsFourTimes(mapList[item]);
                    for (int i = 0; i < 4; i++)
                    {
                        if ((nFlag & (0x01 << i)) == (0x01 << i))
                        {
                            string str = "满足";
                        }
                        else
                        {
                            CsListINF listINF = new CsListINF()
                            {
                                strID = (Litem.Count + 1).ToString(),
                                strName = strParam1 + "_" + strParam2,
                                strDate = item,
                                strTime = LTime[i],
                                strType = "正常签卡",
                                strReason = strParam3
                            };
                            Litem.Add(listINF);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                    {
                        CsListINF listINF = new CsListINF()
                        {
                            strID = (Litem.Count + 1).ToString(),
                            strName = strParam1 + "_" + strParam2,
                            strDate = item,
                            strTime = LTime[i],
                            strType = "正常签卡",
                            strReason = strParam3
                        };
                        Litem.Add(listINF);
                    }
                }
            }
        }

        private static bool IsInTimeRegion(string str0, string str1, string str2)
        {
            bool bIsIn = false;
            if ((DateTime.Compare(Convert.ToDateTime(str1), Convert.ToDateTime(str0)) <= 0)
                && (DateTime.Compare(Convert.ToDateTime(str2), Convert.ToDateTime(str0)) >= 0))
            {
                bIsIn = !bIsIn;
            }
            return bIsIn;
        }

        private static int IsFourTimes(List<string> list)
        {
            int nFlag = 0;
            var xxx = list;
            foreach (var item in list)
            {
                string strDay = item.ToString().Split(' ')[0];
                if (IsInTimeRegion(item.ToString(), strDay + " 7:40", strDay + " 8:30"))
                {
                    string str = "满足上段上班卡";
                    nFlag |= 0x01 << 0;
                }

                if (IsInTimeRegion(item.ToString(), strDay + " 12:00", strDay + " 12:45"))
                {
                    string str = "满足上段下班卡";
                    nFlag |= 0x01 << 1;
                }

                if (IsInTimeRegion(item.ToString(), strDay + " 12:46", strDay + " 13:30"))
                {
                    string str = "满足下段上班卡";
                    nFlag |= 0x01 << 2;
                }

                if (IsInTimeRegion(item.ToString(), strDay + " 18:00", strDay + " 18:15"))
                {
                    string str = "满足下段下班卡";
                    nFlag |= 0x01 << 3;
                }
            }
            return nFlag;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




    }
}
